﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class ChuyenDuLichGUI : Form
    {
        public ChuyenDuLichGUI()
        {
            InitializeComponent();
        }

        public void ShowMaster()
        {
            this.dgvChuyenDL.AutoGenerateColumns = false;
            int tuyenDL = int.Parse(this.cbTuyenDL.SelectedValue.ToString());
            this.dgvChuyenDL.DataSource = ChuyenDuLichBUS.SelectChuyenDuLichByTuyenDL(tuyenDL);
        }

        private void ChuyenDuLichGUI_Load(object sender, EventArgs e)
        {
            this.cbTuyenDL.ValueMember = "MaTuyenDL";
            this.cbTuyenDL.DisplayMember = "TenTuyenDL";
            this.cbTuyenDL.DataSource = TuyenDuLichBUS.SelectTuyenDuLichAll();

            this.ShowMaster();
        }

        private void cbTuyenDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowMaster();
        }

        private void dgvChuyenDL_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgvChuyenDL.CurrentRow != null)
            {
                this.tbMaChuyenDL.Text = this.dgvChuyenDL.CurrentRow.Cells["MaChuyenDL"].Value.ToString();
                this.tbThoiGianKhoiHanh.Text = this.dgvChuyenDL.CurrentRow.Cells["ThoiGianKhoiHanh"].Value.ToString();
                this.tbTongThoiGian.Text = this.dgvChuyenDL.CurrentRow.Cells["TongThoiGian"].Value.ToString();
            }
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            ChuyenDuLichDTO chuyenDL = new ChuyenDuLichDTO();

            chuyenDL.MaChuyenDL = this.tbMaChuyenDL.Text;
            chuyenDL.ThoiGianKhoiHanh = DateTime.Parse(this.tbThoiGianKhoiHanh.Text);
            chuyenDL.TongThoiGian = float.Parse(this.tbTongThoiGian.Text);
            chuyenDL.TuyenDL = int.Parse(this.cbTuyenDL.SelectedValue.ToString());
            bool result = ChuyenDuLichBUS.InsertChuyenDuLich(chuyenDL);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail! Please choose a new MaChuyenDL!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ChuyenDuLichDTO chuyenDL = new ChuyenDuLichDTO();

            chuyenDL.MaChuyenDL = this.tbMaChuyenDL.Text;
            chuyenDL.ThoiGianKhoiHanh = DateTime.Parse(this.tbThoiGianKhoiHanh.Text);
            chuyenDL.TongThoiGian = float.Parse(this.tbTongThoiGian.Text);
            chuyenDL.TuyenDL = int.Parse(this.cbTuyenDL.SelectedValue.ToString());
            bool result = ChuyenDuLichBUS.UpdateChuyenDuLich(chuyenDL);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose new MaChuyenDL,Please!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.dgvChuyenDL.CurrentRow != null)
            {
                String maChuyenDL = this.dgvChuyenDL.CurrentRow.Cells["MaChuyenDL"].Value.ToString();
                DialogResult dr = MessageBox.Show("Do you want to delete ?");
                if (dr == DialogResult.Yes)
                {
                    bool result = ChuyenDuLichBUS.DeleteChuyenDuLichByMaChuyenDL(maChuyenDL);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMaster();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose one employee,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
